/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panel;

import database.DataEngine;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import utils.Debug;

/**
 *
 * @author daz
 */
public class StatisticPanel extends Screen {

    private JLabel timeLabel;
    private JTextField adField;
    private JLabel ccuLabel;
    private JTextArea smsArea;
    private JTextArea cardArea;
    private DataEngine dataEngine;
    private JComboBox[] comboBoxs;
    private JButton updateButton;
    private JButton sendButton;
    private ActionListener updateListener;
    private ActionListener sendListener;
    private static final int DATE = 0;
    private static final int MONTH = 1;
    private static final int YEAR = 2;
    private String[] dateList, yearList, monthList;

    public StatisticPanel(JFrame frame) {
        super(frame);
    }

    @Override
    void initValue() {
        yearList = new String[100];
        monthList = new String[12];
        dateList = new String[31];
        for (int i = 0; i < 100; i++) {
            yearList[i] = String.valueOf(2012 + i);
            if (i < 12) {
                monthList[i] = String.valueOf(i + 1);
            }
            if (i < 31) {
                dateList[i] = String.valueOf(i + 1);
            }
        }
    }

    @Override
    void initComponent() {
        setLayout(null);
        int left = 10;
        int top = 10;
        int height = 20;
        int width = 100;
        int linePadding = 30;
        dataEngine = DataEngine.getInstance();
        ccuLabel = new JLabel();
        ccuLabel.setBounds(left, top, width, height);
        //ad
        top += linePadding;
        timeLabel = new JLabel("Dòng quảng cáo:");
        timeLabel.setBounds(left, top, width, height);
        add(timeLabel);
        top += 30;
        adField = new JTextField();
        adField.setBounds(left, top, 200, height);
        sendButton = new JButton("Gửi");
        sendButton.setBounds(left + 200 + 20, top, width, height);
        sendButton.addActionListener(sendListener);
        //combo
        top += linePadding + 20;
        comboBoxs = new JComboBox[3];
        comboBoxs[DATE] = new JComboBox(dateList);
        comboBoxs[MONTH] = new JComboBox(monthList);
        comboBoxs[YEAR] = new JComboBox(yearList);
        timeLabel = new JLabel("Ngày:");
        timeLabel.setBounds(left, top, width, height);
        add(timeLabel);
        comboBoxs[DATE].setBounds(left + 30, top, width >> 1, height);
        timeLabel = new JLabel("Tháng:");
        timeLabel.setBounds(left + 100, top, width, height);
        add(timeLabel);
        comboBoxs[MONTH].setBounds(left + width + 40, top, width >> 1, height);
        timeLabel = new JLabel("Năm:");
        timeLabel.setBounds(left + 210, top, width, height);
        add(timeLabel);
        comboBoxs[YEAR].setBounds(left + width * 2 + 40, top, width >> 1, height);
        //info area
        int areaWidth = 150;
        int areaHeight = 180;
        top += linePadding + 20;
        smsArea = new JTextArea("");
        smsArea.setBounds(left, top, areaWidth, areaHeight);
        cardArea = new JTextArea("");
        cardArea.setBounds(left + areaWidth + 50, top, areaWidth, areaHeight);
        cardArea.setEditable(false);
        //update button
        top += 30 + areaHeight;
        updateButton = new JButton("Cập nhật");
        updateButton.setBounds(left, top, width, height);
        updateButton.addActionListener(updateListener);

        //
        add(adField);
        add(ccuLabel);
        add(comboBoxs[DATE]);
        add(comboBoxs[YEAR]);
        add(comboBoxs[MONTH]);
        add(smsArea);
        add(cardArea);
        add(updateButton);
        add(sendButton);
        refreshInfo();
    }

    void refreshInfo() {
        ccuLabel.setText("Số người online:" + dataEngine.getNumberOnline());
        String date = (String) comboBoxs[DATE].getSelectedItem();
        if (Integer.parseInt(date) < 10) {
            date = "0" + date;
        }
        String moth = (String) comboBoxs[MONTH].getSelectedItem();
        Debug.d("moth :" + moth);
        if (Integer.parseInt(moth) < 10) {
            moth = "0" + moth;
        }
        String year = (String) comboBoxs[YEAR].getSelectedItem();
//        Debug.d("date :" + date);
        smsArea.setText("");
        smsArea.append("Tin nhắn 8085:" + dataEngine.countSms("8085", date, moth, year) + "\n");
        smsArea.append("Tin nhắn 8585:" + dataEngine.countSms("8585", date, moth, year) + "\n");
        smsArea.append("Tin nhắn 8685:" + dataEngine.countSms("8685", date, moth, year) + "\n");
        smsArea.append("Tin nhắn 8785:" + dataEngine.countSms("8785", date, moth, year) + "\n");
        cardArea.setText("");
        cardArea.append("Thẻ 10000:" + dataEngine.counCard(10000, date, moth, year) + "\n");
        cardArea.append("Thẻ 20000:" + dataEngine.counCard(20000, date, moth, year) + "\n");
        cardArea.append("Thẻ 30000:" + dataEngine.counCard(30000, date, moth, year) + "\n");
        cardArea.append("Thẻ 50000:" + dataEngine.counCard(50000, date, moth, year) + "\n");
        cardArea.append("Thẻ 100000:" + dataEngine.counCard(100000, date, moth, year) + "\n");
        cardArea.append("Thẻ 300000:" + dataEngine.counCard(300000, date, moth, year) + "\n");
        cardArea.append("Thẻ 500000:" + dataEngine.counCard(500000, date, moth, year) + "\n");

        repaint();
    }

    @Override
    void initListenner() {
        updateListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                refreshInfo();
                JOptionPane.showMessageDialog(frame,
                        "Đã cập nhật lại thông tin.");
            }
        };
        sendListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dataEngine.updateAdString(adField.getText());
                JOptionPane.showMessageDialog(frame,
                        "Đã sửa dòng quảng cáo.");
            }
        };
    }
}
